package com.example.paper;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private Button mainBtn1;
    private Button mainBtn2;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//        initView();
//    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void btn_1(View view) {
        Log.i(TAG, "btn_1: ");
//        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PRECISE_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
//            return;
//        }
        String imie = PhoneUtils.getIMEI();
        HashMap<String, String> map = new HashMap<>();
        map.put("imie", imie);
        String json = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        HttpFactory.getInstance().factory(HttpType.SIGNTYPE)
                .getRetrofit().create(Api.class)
                .requestToken(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity resultEntity) {
                        LogUtils.e( "onNext" + resultEntity.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e( "onError" + e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void btn_2(View view) {
        Log.i(TAG, "btn_2: ");
        HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                .getRetrofit().create(Api.class)
                .requestTest().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity resultEntity) {
                        LogUtils.e("onNext" + resultEntity.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e("onError" + e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initView() {
        mainBtn1 = (Button) findViewById(R.id.main_btn1);
        mainBtn2 = (Button) findViewById(R.id.main_btn2);
    }
}
